$(function(){
	
	$('.demo_box_1').popmenu({'background':'#F2F2F2','borderRadius':'6px'});
	$('.demo_box_2').popmenu({'background':'#F2F2F2','borderRadius':'6px'});
	$('.demo_box_3').popmenu({'background':'#F2F2F2','borderRadius':'6px'});
	$('.demo_box_4').popmenu({'background':'#F2F2F2','borderRadius':'6px'});

	$("[data-toggle='tooltip']").tooltip();
	var baseurl = "http://127.0.0.6";
	

	// 初始化画笔颜色按钮事件
    initialize('color', function(color){
    	var penstyle = {'strokeStyle': color};
		note.setPainterStyles(penstyle);
	});

    // 初始化画笔的粗细
	initialize('pensize', function(size){
		var penstyle = {'lineWidth': size};
		note.setPainterStyles(penstyle);
	});

	// 初始化橡皮的大小
	initialize('erasersize', function(size){
		var penstyle = {'size': size};
		note.setEraserStyle(penstyle);
		note.setOperator('draw', 'Eraser');
	});

    // 初始化绘制类型事件
    initialize('shape', function(shape){
		note.setOperator('draw', shape);
	});

	// 初始化控件类型事件
	initialize('widget', function(widget){
		note.setOperator('widget', widget);
	});

	// 初始化相关控件
	function initialize(property, callback){
		$('['+property+']').each(function(index, element){
			element.addEventListener('click', function(){
				callback(element.getAttribute(property));
			});
		});
	}
	
	/**
	 * 将笔记改动至云端
	 * @method savescreenshot
	 * @author yeweiyang 2018-01-07
	 * @return {[type]} [description]
	 */
	
	function savescreenshot(id){
		if(confirm("确定保存吗？")){
			uploadscreenshot(name);
		}
	}
	
	function uploadscreenshot(noteName){
		$('#auth').val(noteName);
		$('#typeId').val(1);
		$('#NoteData').val(JSON.stringify(note.save()));
		var data = note.save();
		console.log(data);
		
		var cacheImg = new Image();
		cacheImg.src = data.cache;
		var bgImg = new Image();
		bgImg.src = data.background;
		note.savedownloadinfo(cacheImg, bgImg, function(imgdata){
			$('#CompressData').val(JSON.stringify(imgdata));
			$.ajax({
				type:"post",
				data:$('#form_save').serialize(),
				url:baseurl+"/file/updateFileById",
				success:function(data){
					if(data == "OK"){
						//$('#modal-save').modal('hide');
						alert("笔记修改成功！");
					}
					else
						alert("笔记修改失败！");
				}
			});
		});
	}
	
	$('#upload').click(function(){
		savescreenshot();
	});
});